package com.flyme.module.order.client.vo;

import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliTransactionType;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.BasePayType;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.wx.api.WxPayConfigStorage;
import com.egzosn.pay.wx.api.WxPayService;
import com.egzosn.pay.wx.bean.WxTransactionType;
import com.flyme.module.order.client.entity.PayAccount;


/**
 * 支付类型
 */
public enum PayType implements BasePayType {

    aliPay {
        /**
         * @return
         */
        @SuppressWarnings({"rawtypes", "unchecked"})
        @Override
        public PayService getPayService(PayAccount payAccount) {
            Long orderId = payAccount.getOrder().getId();
            AliPayConfigStorage configStorage = new AliPayConfigStorage();
            // 配置的附加参数的使用
            configStorage.setAttach(payAccount.getId());
            configStorage.setPid(payAccount.getPartner());
            configStorage.setAppId(payAccount.getAppId());
            configStorage.setKeyPublic(payAccount.getPublicKey());
            configStorage.setKeyPrivate(payAccount.getPrivateKey());
            configStorage.setNotifyUrl(payAccount.getNotifyUrl() + orderId);
            configStorage.setReturnUrl(payAccount.getReturnUrl() + orderId);
            configStorage.setSignType(payAccount.getSignType());
            configStorage.setSeller(payAccount.getSeller());
            configStorage.setPayType(payAccount.getPayType().toString());
            configStorage.setMsgType(payAccount.getMsgType());
            configStorage.setInputCharset(payAccount.getInputCharset());
            configStorage.setTest(payAccount.getIsTest());
            // 请求连接池配置
            HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
            // 最大连接数
            httpConfigStorage.setMaxTotal(20);
            // 默认的每个路由的最大连接数
            httpConfigStorage.setDefaultMaxPerRoute(10);
            return new AliPayService(configStorage, httpConfigStorage);
        }

        @Override
        public TransactionType getTransactionType(String transactionType) {
            // com.egzosn.pay.ali.before.bean.AliTransactionType
            // 17年更新的版本,旧版本请自行切换

            // AliTransactionType 17年更新的版本,旧版本请自行切换
            return AliTransactionType.valueOf(transactionType);
        }

    },
    wxPay {
        @Override
        public PayService<?> getPayService(PayAccount payAccount) {
            Long orderId = payAccount.getOrder().getId();
            WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
            wxPayConfigStorage.setMchId(payAccount.getPartner());
            wxPayConfigStorage.setAppid(payAccount.getAppId());
            // 转账公钥，转账时必填
            wxPayConfigStorage.setKeyPublic(payAccount.getPublicKey());
            wxPayConfigStorage.setSecretKey(payAccount.getPrivateKey());
            wxPayConfigStorage.setNotifyUrl(payAccount.getNotifyUrl() + orderId);
            wxPayConfigStorage.setReturnUrl(payAccount.getReturnUrl() + orderId);
            wxPayConfigStorage.setSignType(payAccount.getSignType());
            wxPayConfigStorage.setPayType(payAccount.getPayType().toString());
            wxPayConfigStorage.setMsgType(payAccount.getMsgType());
            wxPayConfigStorage.setInputCharset(payAccount.getInputCharset());
            wxPayConfigStorage.setTest(payAccount.getIsTest());

            // https证书设置 方式一
            /*
             * HttpConfigStorage httpConfigStorage = new HttpConfigStorage();
             * //TODO 这里也支持输入流的入参。 //
             * httpConfigStorage.setKeystore(PayType.class.getResourceAsStream(
             * "/证书文件")); httpConfigStorage.setKeystore("证书信息串");
             * httpConfigStorage.setStorePassword("证书密码"); //是否为证书地址
             * httpConfigStorage.setPath(false); return new
             * WxPayService(wxPayConfigStorage, httpConfigStorage);
             */
            return new WxPayService(wxPayConfigStorage);
        }

        /**
         * 根据支付类型获取交易类型
         */
        @Override
        public TransactionType getTransactionType(String transactionType) {

            return WxTransactionType.valueOf(transactionType);
        }
    };

    public abstract PayService<?> getPayService(PayAccount payAccount);


}
